package com.sunspring.zdcall.aop;

import com.sunspring.zdcall.common.SimpleResponse;
import com.sunspring.zdcall.common.exception.AuthException;
import com.sunspring.zdcall.common.exception.ZdException;
import lombok.extern.slf4j.Slf4j;
import org.quartz.SchedulerException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 全局异常捕获链
 * @Author WuLei
 * @Date 2018/11/27  14:22
 */
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    /**
     * 捕获ZdException
     * @param ex ZdException
     * @return SimpleResponse(0,msg)
     */
    @ExceptionHandler(ZdException.class)
    @ResponseBody
    public SimpleResponse catchZdException(ZdException ex) {
        log.error(ex.getMessage());
        return SimpleResponse.fail(ex.getMessage());
    }

    /**
     * SchedulerException
     * @param ex ZdException
     * @return SimpleResponse(0,msg)
     */
    @ExceptionHandler(SchedulerException.class)
    @ResponseBody
    public SimpleResponse catchSchedulerException(SchedulerException ex) {
        log.error(ex.getMessage());
        return SimpleResponse.fail("操作定时任务失败");
    }


    /**
     * AuthException
     * @param ex ZdException
     * @return SimpleResponse(0,msg)
     */
    @ExceptionHandler(AuthException.class)
    public String catchAuthException(AuthException ex) {
        return "/login";
    }


    /**
     * 捕获Exception
     * @param ex ZdException
     * @return SimpleResponse(0,msg)
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public SimpleResponse catchEx(Exception ex) {
        ex.printStackTrace();
        return SimpleResponse.fail("服务端异常");
    }
}
